package com.pureut.system.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.system.domain.SysBusinessFlow;
import com.pureut.system.domain.SysBusinessRule;
import com.pureut.system.domain.vo.SysBusinessFlowVo;
import com.pureut.system.domain.vo.SysBusinessRuleVo;
import com.pureut.system.service.ISysBusinessFlowService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @Author: pureut
 * @Date: 2025-04-07 18:14:24
 * @Desc: 业务流程Controller
 */
@RestController
@RequestMapping("/system/sysBusinessFlow")
public class SysBusinessFlowController extends BaseController {

    /**
     * 业务流程Service
     */
    @Resource
    private ISysBusinessFlowService sysBusinessFlowService;



    /**
     * 获取业务流程详细信息
     * @param: id 业务流程主键
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("sysBusinessFlow-query")
    @GetMapping(value = "/info")
    public AjaxResult getInfo() {
        List<SysBusinessFlow> list = sysBusinessFlowService.list();
        List<Long> idList=Arrays.asList(1L,2L,3L,4L,6L,7L);
        list.forEach(e->{
            List<String> statusList=new ArrayList<>();
            if(idList.contains(e.getId())){
                statusList.add("新增");
                statusList.add("提交");
                statusList.add("审核");
            }
            if(e.getId()==3){
                statusList.add("合同上传");
            }
            if(e.getId()==5){
                statusList.add("新增");
                statusList.add("生产确认");
            }
            e.setButton(getInfo2(Arrays.asList(e.getIsFlag().split(",")),statusList));
        });
        return AjaxResult.success(list);
    }
    private static List<Map<String,String>> getInfo2(List<String> list,List<String> statusList){
        List<Map<String,String>> result=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Map<String,String> maps=new HashMap<>();
            maps.put("lable",statusList.get(i));
            maps.put("value",list.get(i));
            result.add(maps);
        }
        return result;
    }




    /**
     * 修改业务流程
     * @param: sysBusinessFlow 业务流程对象
     * @return: Json工具类对象
     */
    @PutMapping
    public AjaxResult edit( @RequestBody List<SysBusinessFlowVo> sysBusinessFlowVo) {
        List<SysBusinessFlow> collect = sysBusinessFlowVo.stream().map(e -> {
            SysBusinessFlow sysBusinessFlow = new SysBusinessFlow();
            String result = e.getButton().stream()
                    .flatMap(map -> map.entrySet().stream()
                            .filter(entry -> !"lable".equals(entry.getKey()))
                            .map(Map.Entry::getValue)
                    )
                    .collect(Collectors.joining(","));
            sysBusinessFlow.setIsFlag(result);
            sysBusinessFlow.setId(e.getId());
            sysBusinessFlow.setTitle(e.getTitle());
            return sysBusinessFlow;
        }).collect(Collectors.toList());

        return toAjax(sysBusinessFlowService.updateBatchById(collect));

    }


}
